package com.kalix.ksh.proint.biz;


import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.biz.ITestService;
import com.kalix.ksh.proint.api.dao.ITestBeanDao;
import com.kalix.ksh.proint.api.model.InfUtil;
import com.kalix.ksh.proint.entities.TestBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class TestServiceImpl extends GenericBizServiceImpl<ITestBeanDao, TestBean> implements ITestService {
    public TestServiceImpl(){
        super.init(TestBean.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort = (String) ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    @Override
    public String getValue() {
        String sql = "select ru from TestBean ru";
        List<TestBean> list = dao.find(sql);
        JSONArray str = new JSONArray();
        for(int i = 0;i<list.size();i++){
            String uuid = list.get(i).getUuid();
            String name = list.get(i).getName();
            String url = "http://"+wsIp+":"+wsPort+"/metrics/Metrics.moPath=\"Line.domain=\"defaultEngine\",uuid=\""+uuid+"\"\",name=\"ifPercent\"?fresh=1860 ";
            double number = getData(url);
            String url2 = "http://"+wsIp+":"+wsPort+"/metrics/Metrics.moPath=\"Line.domain=\"defaultEngine\",uuid=\""+uuid+"\"\",name=\"ifOctets\"?fresh=1860 ";
            double number2 = getData(url2);
            JSONObject jobj = new JSONObject();
            jobj.put("name",name);
            jobj.put("version",number);
            jobj.put("location",number2);
            str.put(jobj);
        }
        JSONObject obj = new JSONObject();
        obj.put("titile","骨干路线的实时运行情况");
        JSONArray thead = new JSONArray();
        JSONObject obj1 = new JSONObject();
        obj1.put("name","设备名称");
        obj1.put("width","100px");
        JSONObject obj2 = new JSONObject();
        obj2.put("name","总流量(M)");
        obj2.put("width","100px");
        JSONObject obj3 = new JSONObject();
        obj3.put("name","带宽占用比(%)");
        obj3.put("width","100px");
        thead.put(obj1);
        thead.put(obj2);
        thead.put(obj3);
        obj.put("thead",thead);
        obj.put("data",str);
        String stri = obj.toString();
        return stri;
    }


    public double getData(String url){
        String data = InfUtil.interfaceUtil(url,"");
        JSONObject obj = new JSONObject(data);
        double min = obj.getJSONObject("value").getDouble("value");
        return min;
    }
}
